package com.osce.score.controller;

import com.csce.domain.entity.score.ScoreExExam;
import com.csce.domain.entity.score.ScoreExExamSession;
import com.csce.domain.entity.score.dto.ScoreScoreDto;
import com.csce.domain.entity.score.vo.ScoreScoreVo;
import com.osce.common.annotation.Log;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.core.page.TableDataInfo;
import com.osce.common.enums.BusinessType;
import com.osce.common.utils.poi.ExcelUtil;
import com.osce.interfaces.score.IScoreExExamService;
import com.osce.interfaces.score.IScoreExExamSessionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考试场次Controller
 *
 * @author zzx
 * @since 2023-09-07
 */
@RestController
@RequestMapping("/score/ex_score_exam_session")
public class ScoreExExamSessionController extends BaseController {

    @Resource private IScoreExExamSessionService scoreExExamSessionService;
    @Resource private IScoreExExamService exExamService;

    /**
     * 查询考试场次列表
     */
    @PreAuthorize("@ss.hasPermi('score:ex_score_exam_session:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScoreExExamSession scoreExExamSession) {
        startPage();
        List<ScoreExExamSession> list = scoreExExamSessionService.selectScoreExExamSessionList(scoreExExamSession);
        return getDataTable(list);
    }

    /**
     * 导出考试场次列表
     */
    @PreAuthorize("@ss.hasPermi('score:ex_score_exam_session:export')")
    @Log(title = "考试场次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScoreExExamSession scoreExExamSession) {
        List<ScoreExExamSession> list = scoreExExamSessionService.selectScoreExExamSessionList(scoreExExamSession);
        ExcelUtil<ScoreExExamSession> util = new ExcelUtil<ScoreExExamSession>(ScoreExExamSession.class);
        util.exportExcel(response, list, "考试场次数据");
    }

    /**
     * 获取考试场次详细信息
     */
    @PreAuthorize("@ss.hasPermi('score:ex_score_exam_session:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(scoreExExamSessionService.selectScoreExExamSessionById(id));
    }

    /**
     * 新增考试场次
     */
    @PreAuthorize("@ss.hasPermi('score:ex_score_exam_session:add')")
    @Log(title = "考试场次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScoreExExamSession scoreExExamSession) {
        return toAjax(scoreExExamSessionService.insertScoreExExamSession(scoreExExamSession));
    }

    /**
     * 修改考试场次
     */
    @PreAuthorize("@ss.hasPermi('score:ex_score_exam_session:edit')")
    @Log(title = "考试场次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScoreExExamSession scoreExExamSession) {
        return toAjax(scoreExExamSessionService.updateScoreExExamSession(scoreExExamSession));
    }

    /**
     * 重置考试场次考官信息
     */
    @PreAuthorize("@ss.hasPermi('score:ex_score_exam_session:edit')")
    @Log(title = "考试场次", businessType = BusinessType.UPDATE)
    @PutMapping("/clear_examiner/{id}")
    public AjaxResult edit(@PathVariable("id") Long id) {
        return toAjax(scoreExExamSessionService.clearExaminer(id));
    }

    /**
     * 删除考试场次
     */
    @PreAuthorize("@ss.hasPermi('score:ex_score_exam_session:remove')")
    @Log(title = "考试场次", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(scoreExExamSessionService.deleteScoreExExamSessionByIds(ids));
    }

    /**
     * 获取考试成绩列表
     */
    @PreAuthorize("@ss.hasPermi('score:performance:list')")
    @GetMapping("/exam_score_list")
    public TableDataInfo getExamScoreList(@Validated ScoreScoreDto scoreScoreDto) {
        startPage();
        return getDataTable(scoreExExamSessionService.getExamScoreList(scoreScoreDto));
    }

    /**
     * 获取考试评分表详情
     */
    @PreAuthorize("@ss.hasPermi('score:performance:info')")
    @GetMapping("/select_allot_sheet_parse/{markId}")
    public AjaxResult selectAllotSheetParse(@PathVariable("markId") Long markId) {
        return success(scoreExExamSessionService.selectAllotSheetParse(markId));
    }

    /**
     * 导出考试成绩清单
     */
    @PreAuthorize("@ss.hasPermi('score:performance:export')")
    @PostMapping("/performance-export")
    public void performanceExport(HttpServletResponse response, @Validated ScoreScoreDto scoreScoreDto) {
        ScoreExExam scoreExExam = exExamService.selectScoreExExamById(scoreScoreDto.getExamId());
        List<ScoreScoreVo> list = scoreExExamSessionService.getExamScoreList(scoreScoreDto);
        ExcelUtil<ScoreScoreVo> util = new ExcelUtil<>(ScoreScoreVo.class);
        util.exportExcel(response, list, scoreExExam.getName());
    }
}
